<template>
    <div class="coupon">
        <search-form :searchArray="searchArray" :searchObj="searchObj" @search="getList">
            <el-button class="btn search mL20" @click="add">新增</el-button>
        </search-form>
        <elTable :formData="formData" @getList="getList">
                <el-table-column prop="name" label="优惠劵名称" align="center"></el-table-column>
                <el-table-column prop="createTime" label="创建时间" align="center">
                    <template slot-scope="scope">
                        {{$normalTime(scope.row.createTime)}}
                    </template>
                </el-table-column>
                <el-table-column prop="name" label="优惠劵名称" align="center"></el-table-column>
                <el-table-column prop="name" label="有效时间" align="center">
                    <template slot-scope="scope">
                        {{scope.row.startTime && $normalTime(scope.row.startTime)+ '-' || ''}}{{scope.row.endTime && $normalTime(scope.row.endTime)}}
                    </template>
                </el-table-column>
                <el-table-column prop="typeMessage" label="优惠劵类型" align="center"></el-table-column>
                <el-table-column prop="statusMessage" label="状态" align="center"></el-table-column>
                <el-table-column prop="action" label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" @click="clock(scope.row)" v-if="scope.row.status != 1">{{scope.row.status == 0 ? '上架' : '下架'}}</el-button>
                        <el-button type="text"  @click="editer(scope.row)">编辑</el-button>
                    </template>
                </el-table-column>
        </elTable>
    </div>
</template>
<script>
import { mapGetters } from 'vuex'
import elTable from "@/components/xForm/elTable"
export default {
    name: 'coupon',
    components: {
        elTable
    },
    data () {
        return {
            visible: {
                show: false
            },
            formData: {
                currentPage: 1,
                pageSize: 10,
                data: [
                ],
                total: 0
            },
            searchObj: {
                name: '',
                status: '',
                receiveStartTime: '',
                receiveEndTime: '',
                startTime: '',
                endTime: '',
                type: ''
            },
            searchArray: [
                {
                    type: 'input',
                    placeholder: '请输入优惠劵名称',
                    value: '',
                    label: '优惠劵名称',
                    key: 'name'
                },
                {
                    type: "select",
                    placeholder: "请选择",
                    filterable: false,
                    value: "",
                    width: '',
                    label: '优惠劵类型',
                    key: 'type',
                    list: [
                    ]
                },
                {
                    type: "select",
                    placeholder: "请选择",
                    filterable: false,
                    value: "",
                    width: '',
                    label: '状态',
                    key: 'status',
                    list: [
                    ]
                },
                {
                    types: 'datetimerange',
                    type: 'datePicker',
                    label: '领取时间',
                    value: '',
                    startText: '开始时间',
                    endText: '结束时间',
                    rangeSpeparator: "至",
                    key1: 'receiveStartTime',
                    key2: 'receiveEndTime'
                },
                {
                    types: 'datetimerange',
                    type: 'datePicker',
                    value: '',
                    label: '有效时间',
                    startText: '开始时间',
                    endText: '结束时间',
                    rangeSpeparator: "至",
                    key1: 'startTime',
                    key2: 'endTime'
                },
            ],
        }
    },
    computed: {
        ...mapGetters({
            enumAll: 'common/getEnumAll'
        }),
    },
    created () {
        this.init()
    },
    methods: {
        init () {
            this.$set(this.searchArray[1], 'list', this.enumAll.EncumCouponType || [])
            this.$set(this.searchArray[2], 'list', this.enumAll.PutAwayStatus || [])
            this.getList()
        },
        getList(status = true) {
            if (!status) this.formData.currentPage = 1
            const {formData: {currentPage, pageSize}, searchObj} = this
            let param = {}
            param = {
                currentPage,
                pageSize
            }
            for ( let k in searchObj) {
                if (searchObj[k] !== '') {
                    param[k] = searchObj[k]
                }
            }
            this.$loading(true)
            this.$api.activity.getCouponList(param).then(r => {
                this.formData.data = r.data && r.data.list || []
                this.formData.total = r.data.total || 0
                console.log(this.formData)
            }).finally(() => {
                this.$loading(false)
            })
        },
        clock (item) {
            this.$confirm(`您确定要${item.status == 0 ? '上架' : '下架'}?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
                }).then(() => {
                    let param = {}
                    param = {
                        id: item.id,
                        status: item.status == -1 ? 0 : -1
                    }
                    this.$api.activity.putChangeStatusCoupon(param).then(r => {
                        this.getList()
                    })
                }).catch(() => {
            })
        },
        editer (item) {
            this.$router.push({
                path: '/activity/coupon/detail/1/'+item.id
            })
        },
        add () {
            this.$router.push({
                path: '/activity/coupon/detail/0/0'
            })
        }
    }
}
</script>
<style lang="scss" scoped>
.coupon {
    .search-form {
        margin-bottom: 16px;
    }
}
</style>